package com.study.d012Components;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
*@description 滚动条
*@author xtayhame
*date 2020年3月3日
*version
*since v1.0
*/
public class ScrollBarMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane ap = new AnchorPane();

		Button addButton = new Button("滑动");

		VBox vb = new VBox();
		for(int i=0;i<15;i++){
			Button button = new Button("button" + i);
			vb.getChildren().add(button);
		}

		ScrollBar sb = new ScrollBar();
		//设置滚动条的方向为垂直，默认为水平
		sb.setOrientation(Orientation.VERTICAL);
		//设置每次滑动的距离
		sb.setUnitIncrement(10);
		//设置滚动条可见组件的大小
		sb.setVisibleAmount(100);
		//设置滚动条的值
		sb.setValue(50);

		//监听滚动条的滑动值， 默认0--100
		sb.valueProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				System.out.println(newValue);
				//滑动滚动条时，VBox跟着滑动
				vb.setLayoutY(-newValue.doubleValue());
			}
		});


		addButton.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				sb.setUnitIncrement(50);
				//设置滚动条的增量值
				sb.increment();

				//sb.decrement();
			}
		});

		ap.getChildren().addAll(vb, sb, addButton);
		AnchorPane.setLeftAnchor(vb, 10.0);
		AnchorPane.setLeftAnchor(sb, 100.0);
		AnchorPane.setLeftAnchor(addButton, 200.0);
		AnchorPane.setTopAnchor(addButton, 100.0);


		stage.setScene(new Scene(ap, 400, 500));
		stage.show();

		//设置滚动条的高度和VBox的高度相等
		sb.setPrefHeight(vb.getHeight());
		//设置滚动条的最大值
		sb.setMax(vb.getHeight());

		//设置滚动条的宽和高
		sb.setPrefWidth(vb.getWidth());
		sb.setPrefHeight(vb.getHeight());

	}
}
